/*
 *  new.c
 *  ooc
 *
 *  Created by hume on 08-5-3.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "new.h"
/*
 * create a new class.
 * return the pointer which point to the new class's mem addr.
 */
void * new(const void *_class,...)
{
     const struct Class *class=_class;
     void *p=calloc(1,class->size);
     assert(p);
     *(const struct Class **)p=class;

     if(class->ctor)
     {
	  va_list ap;
	  va_start(ap,_class);
	  p=class->ctor(p,&ap);
	  va_end(ap);
     }
     return p;
}

/* delete the object pointed by self. */
void delete(void *self)
{
     const struct Class **cp=self;
     if(self && *cp && (*cp)->dtor)
	  self=(*cp)->dtor(self);
     free(self);
}

int differ(void *self,const void *b)
{
     const struct Class * const * cp=self;

     assert(self && *cp && (*cp)->differ);
     return (*cp)->differ(self,b);
}

size_t sizeOf(const void *self)
{
     const struct Class *const *cp=self;

     assert(self && *cp);
     return (*cp)->size;
}

void * clone(const void *self)
{
     const struct Class *const *cp=self;
     assert(self && *cp);
     return (*cp)->clone(self);
}
